package com.ph.shopping.facade.permission.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ph.shopping.common.core.base.BaseService;
import com.ph.shopping.common.core.constant.CommonConstants;
import com.ph.shopping.common.core.customenum.ResultEnum;
import com.ph.shopping.common.core.exception.BizException;
import com.ph.shopping.common.util.page.PageBean;
import com.ph.shopping.common.util.result.Result;
import com.ph.shopping.facade.mapper.WarehouseAddressMapper;
import com.ph.shopping.facade.permission.entity.WarehouseAddress;
import com.ph.shopping.facade.permission.service.IWarehouseAddressService;
import com.ph.shopping.facade.permission.vo.WarehouseAddressVo;

/**
 * 
 * @项目：phshopping-service-permission
 *
 * @描述：仓库地址接口实现
 *
 * @作者： liuy
 *
 * @创建时间：2017年3月27日
 *
 * @Copyright @2017 by liuy
 */
@Component
@Service(version="1.0.0")
public class WarehouseAddressImpl extends BaseService  implements IWarehouseAddressService{

	//仓库地址Mapper
    @Autowired
    WarehouseAddressMapper warehouseAddressMapper;
    
    /**
     * 分页获取仓库地址列表
     * @param pageIndex 当前页码
     * @param pageSize 每页条数
     * @param warehouseAddressVo 查询条件
     * @return List<WarehouseAddressVo>
     */
	@Override
	public Result getWarehouseAddressVoPageList(PageBean page, WarehouseAddressVo warehouseAddressVo) throws BizException {
		PageHelper.startPage(page.getPageNum(), page.getPageSize());
		List<WarehouseAddressVo> list=(Page<WarehouseAddressVo>)warehouseAddressMapper.getWarehouseAddressVoListByPage(warehouseAddressVo);
		PageInfo<WarehouseAddressVo> pageInfo=new PageInfo<WarehouseAddressVo>(list);
		return getResult(ResultEnum.SUCCESS,pageInfo.getList(),pageInfo.getTotal());
	}

    /**
     * 获取仓库地址列表
     * @param warehouseAddressVo 查询条件
     * @return List<WarehouseAddressVo>
     */
	@Override
	public List<WarehouseAddressVo> getWarehouseAddressVoList(WarehouseAddress warehouseAddress) throws BizException {
		List<WarehouseAddressVo> list = warehouseAddressMapper.getWarehouseAddressVoList(warehouseAddress);
		return list;
	}

    /**
     * 新增仓库地址
     * @param warehouseAddress 仓库地址
     * @return Result
     */
	@Override
    @Transactional(rollbackFor=Exception.class)
	public Result addWarehouseAddress(WarehouseAddress warehouseAddress) throws BizException {
		
		//如果为默认地址，更新其他地址为非默认地址
		if(CommonConstants.IS_YES_DEFAULT==warehouseAddress.getIsDefault()){
	    	warehouseAddressMapper.updateOtherNotDefault(warehouseAddress);
		}
		//如果为提货点，更新其他地址为非提货点
		if(CommonConstants.IS_YES_DELIVERYPOINT==warehouseAddress.getIsDeliveryPoint()){
	    	warehouseAddressMapper.updateOtherNotDeliveryPoint(warehouseAddress);
		}
		warehouseAddress.setCreateTime(new Date());
		warehouseAddress.setUpdateTime(new Date());
		warehouseAddressMapper.insert(warehouseAddress);
		return getResult(ResultEnum.SUCCESS);
	}

    /**
     * 修改仓库地址
     * @param warehouseAddress 仓库地址
     * @return Result
     */
	@Override
    @Transactional(rollbackFor=Exception.class)
	public Result updateWarehouseAddress(WarehouseAddress warehouseAddress) throws BizException {

		//如果为默认地址，更新其他地址为非默认地址
		if(CommonConstants.IS_YES_DEFAULT==warehouseAddress.getIsDefault()){
	    	warehouseAddressMapper.updateOtherNotDefault(warehouseAddress);
		}
		//如果为提货点，更新其他地址为非提货点
		if(CommonConstants.IS_YES_DELIVERYPOINT==warehouseAddress.getIsDeliveryPoint()){
	    	warehouseAddressMapper.updateOtherNotDeliveryPoint(warehouseAddress);
		}
		warehouseAddress.setUpdateTime(new Date());
		warehouseAddressMapper.updateByPrimaryKeySelective(warehouseAddress);
		return getResult(ResultEnum.SUCCESS);
	}

    /**
     * 删除仓库地址
     * @param warehouseAddress 仓库地址
     * @return Result
     */
	@Override
	public Result batchDeleteWarehouseAddress(List<Long> list) throws BizException {
		
		//逻辑删除仓库地址信息
		warehouseAddressMapper.batchDeleteWarehouseAddress(list);
        return getResult(ResultEnum.SUCCESS);
	}

    /**
     * 通过id获取仓库地址详情
     * @param warehouseAddressVo 查询条件
     * @return WarehouseAddressVo
     */
	@Override
	public WarehouseAddressVo getWarehouseAddressVoDetail(WarehouseAddressVo warehouseAddressVo) throws BizException {
		WarehouseAddressVo warehouseAddressDetail = warehouseAddressMapper.getWarehouseAddressDetail(warehouseAddressVo);
		return warehouseAddressDetail;
	}

    /**
     * 根据Id查询仓库地址
     * @param warehouseAddress 仓库地址
     * @return WarehouseAddress
     */
	@Override
	public WarehouseAddress getWarehouseAddressById(WarehouseAddress warehouseAddress)  throws BizException {
		return warehouseAddressMapper.selectByPrimaryKey(warehouseAddress);
	}

    /**
     * 设置仓库地址为默认地址
     * @param warehouseAddress 仓库地址
     */
	@Override
    @Transactional(rollbackFor=Exception.class)
	public void updateWarehouseDefault(WarehouseAddress warehouseAddress)  throws BizException {
    	logger.debug("设置仓库地址为默认地址");

    	//更新其他地址为非默认地址
    	warehouseAddressMapper.updateOtherNotDefault(warehouseAddress);
		//更新设置仓库地址为默认地址
    	warehouseAddressMapper.updateByPrimaryKeySelective(warehouseAddress);
	}

    /**
     * 设置仓库地址为提货点
     * @param warehouseAddress 仓库地址
     */
	@Override
	public void updateWarehouseDeliveryPoint(WarehouseAddress warehouseAddress)  throws BizException {
		logger.debug("设置仓库地址为默认地址");
		//更新其他地址为非提货点
    	warehouseAddressMapper.updateOtherNotDeliveryPoint(warehouseAddress);
		//设置仓库地址为提货点
    	warehouseAddressMapper.updateByPrimaryKeySelective(warehouseAddress);
	}

}
